package com.gzdh.dishrecognition.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.Zweekmenubak;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.view.HorizontalListView;

import java.util.List;

public class HorizontalListViewAdapter extends BaseAdapter {

    private Context mContext;


    private List<String> weekList;

    private int select = 0;

    private GvOnClick mGvOnClick;
    private HorizontalListView horizontalListView;


    public void setmGvOnClick(GvOnClick mGvOnClick) {
        this.mGvOnClick = mGvOnClick;
    }

    public HorizontalListViewAdapter(Context context, HorizontalListView horizontalListView) {
        mContext = context;
        this.horizontalListView = horizontalListView;
    }

    public void setWeekList(List<String> weekList) {
        this.weekList = weekList;
    }


    @Override
    public int getCount() {
        return weekList.size();
    }

    @Override
    public Object getItem(int position) {
        return weekList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.view_string_gridview_item2, null);
            holder = new ViewHolder();
            holder.nameTv = (TextView) convertView.findViewById(R.id.nameTv);
            holder.mainLinear = (LinearLayout) convertView.findViewById(R.id.mainLinear);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.nameTv.setText(weekList.get(position));
        if (position == select) {
            holder.mainLinear.setBackgroundColor(Color.rgb(200, 200, 200));
        } else {
            holder.mainLinear.setBackgroundColor(Color.rgb(255, 255, 255));
        }
        horizontalListView.setOnItemClickListener((adapterView, view, positionI, id) -> {
            if (mGvOnClick != null) {
                mGvOnClick.userClick(weekList.get(positionI));
            }
            select = positionI;
            notifyDataSetChanged();
        });
        return convertView;
    }

    static class ViewHolder {
        public TextView nameTv;
        private LinearLayout mainLinear;
    }

    public interface GvOnClick {
        void userClick(String object);
    }

}
